<template>
  <div class>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" :close-on-click-modal="false" @close="close">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="密码" v-if="type=='password'">
          <el-input v-model="form.passWord" type="password" autocomplete="off" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" v-if="type=='password'" placeholder="请输入新密码">
          <el-input v-model="cPassWord" type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" v-if="type=='password'" placeholder="请确认新密码">
          <el-input v-model="qCPassWord" type="password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="userTel" v-if="type=='userTel'">
          <el-input v-model.trim="form.userTel" autocomplete="off" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="close">取 消</el-button>
        <el-button size="medium" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userSave } from '@/api/setup';

export default {
  name: 'goodlistedit',
  created() {},
  data() {
    return {
      form: {
        id: '',
        passWord: '',
        userTel: ''
      },
      cPassWord: '',
      qCPassWord: '',
      data: {},
      type: '',
      title: '',
      dialogFormVisible: false
    };
  },
  methods: {
    showEdit(row, type) {
      if (!row) {
        this.title = '添加';
      } else {
        if (type == 'userTel') {
          this.title = '修改手机号';
        } else {
          this.title = '修改密码';
        }
        this.type = type;
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      // this.$refs["form"].resetFields();
      // this.form = this.$options.data().form;
      this.dialogFormVisible = false;
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          let params = {};
          if (this.type == 'password') {
            if (this.cPassWord !== this.qCPassWord) {
              this.$baseMessage('输入的新密码不一致', 'error');
              return;
            } else {
              params.passWord = this.qCPassWord;
              params.id = this.form.id;
            }
          } else if (this.type == 'userTel') {
            params.userTel = this.form.userTel;
            params.id = this.form.id;
          }
          const { msg } = await userSave(params);
          this.$baseMessage(msg, 'success');
          this.$emit('fetchData');
          this.close();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
